import request from '@/utils/request';
import { encrypt } from '@bud-fe/utils';
import type { IGetAuthDataResp, IResetPwdParams, IUserInfoResp } from './types';

const systemService = {
  /**
   * 查询当前登录用户的菜单&按钮权限
   * @returns
   */
  getAuthData() {
    return request.get<IGetAuthDataResp>(
      '/abi-cloud-middle-platform-user-service/console/resource/select-tree-by-user-roles-V2',
      {},
      // TODO: systemId 待修改
      { headers: { systemId: 1 } },
    );
  },
  /**
   * 查询当前用户详情（包含角色信息、权限信息，需要登录，用户ID从TOKEN获取）
   * @returns
   */
  async getUserInfo() {
    const res = await request
      .get<IUserInfoResp>('/abi-cloud-middle-platform-user-service/business/user-info/query-current-user')
      .catch(() => null);
    return res?.data;
  },
  /**
   * 登录后-重置密码
   * @param params
   * @returns
   */
  resetPwd(params: IResetPwdParams) {
    const encryptPwd = encrypt(params.firstPassword);
    return request.post<IAPIResponse>('/abi-cloud-middle-platform-user-service/business/password/reset/login', {
      ...params,
      currentPassword: encrypt(params.currentPassword),
      firstPassword: encryptPwd,
      secondPassword: encryptPwd,
      // TODO: systemId 待传。默认为: 1-中台console
    });
  },
  /**
   * 退出登录（需要传token）
   * @returns
   */
  logout() {
    return request.post<IAPIResponse>('/abi-cloud-middle-platform-auth-service/login/logout');
  },
};

export default systemService;
